package com.woniuxy.controller;

import com.woniuxy.result.Result;
import com.woniuxy.util.JWTUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController {

    @RequestMapping("login")
    public Result login(@RequestParam String uname, @RequestParam String password, HttpServletResponse response){
         if(uname.equals("admin")&&password.equals("1234")){
             String token = JWTUtil.generateToken(1000L,uname);
             System.out.println(token);
             response.setHeader("token",token);
             //前后端分离的项目，必须暴露响应头，否则前端收不到响应头
             response.setHeader("Access-Control-Expose-Headers","token");
             //封装响应数据
             return Result.success().msg("登录成功！");
         }
         return Result.failed().msg("登录失败");
    }
}
